<?php
class EventSubscriber extends EventsAppModel {

	var $name = 'EventSubscriber';

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $belongsTo = array(
		'Event' => array(
			'className' => 'Events.Event',
			'foreignKey' => 'event_id',
		),
		'User' => array(
			'className' => 'Users.User',
			'foreignKey' => 'user_id',
		)
	);
	
	function getSubscribers($eventId, $includeEventModelData = false)
	{
		if($includeEventModelData === false)
		{
			$this->unbindModel(array("belongsTo" => array("className" => "Event")), true);
		}
		return $this->find("all",array("conditions"=>array($this->alias.".event_id"=>$eventId),"recursive"=>0));
	}
	
	function isSubscriber($eventId, $userId)
	{
		return $this->hasAny(array($this->alias.".event_id" => $eventId, $this->alias.".user_id"=>$userId));
	}
	
	function addSubscriber($eventId, $userId, $email = false, $userName = false)
	{
		$result = false;
		$user = $this->User->findById($userId);
		if($this->Event->hasAny(array("Event.id"=>$eventId)) && $userId && !empty($user))
		{
			if($email == false)
			{
				$email = $user["User"]["email"];
			}
			if($userName == false)
			{
				$userName = $user["User"]["display_name"];
			}
			$this->create();
			$result = $this->save(array($this->alias => array("event_id" => $eventId, "user_id" => $userId, "email"=>$email, "name"=>$userName)));
		}
		return $result;
	}
	
	function removeSubscriber($eventId, $userId)
	{
		$result = false;
		if($this->hasAny(array("event_id"=>$eventId, "user_id"=>$userId)))
		{
			$result = $this->deleteAll(array("event_id"=>$eventId, "user_id"=>$userId));
		}
		return $result;
	}

}
?>